﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
using Microsoft.Phone;

namespace ManYor.Phone.Common
{
    /// <summary>
    /// 文件存储类
    /// </summary>
    public class IsolateCache
    {
        private static IsolatedStorageFile _pageIsoStoFile;
        /// <summary>
        /// 存储
        /// </summary>
        public IsolateCache()
        {
            _pageIsoStoFile = IsolatedStorageFile.GetUserStoreForApplication();
            if (!_pageIsoStoFile.DirectoryExists("cache"))
            {
                _pageIsoStoFile.CreateDirectory("cache");
            }
        }
        /// <summary>
        /// 返回独立存储对象
        /// </summary>
        public IsolatedStorageFile CacheFileAccess
        {
            get { return _pageIsoStoFile; }
        }
        /// <summary>
        /// 缓存路径
        /// </summary>
        public string CachePath(string key)
        {
            return "cache\\" + key;
        }   
        /// <summary>
        /// 检查是否包含指定的缓存文件
        /// </summary>
        /// <param name="cachekey"></param>
        /// <returns></returns>
        public bool ContainKey(string cachekey)
        {
            //return IsolatedStorageSettings.ApplicationSettings.Contains(cachekey);
            ///TODO:
            return _pageIsoStoFile.FileExists("cache\\" + cachekey);
        }
        /// <summary>
        /// 从缓存中读取图片,如不存在，则返回null
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public BitmapImage GetImageFormCache(string key)
        {
            if (_pageIsoStoFile.FileExists("cache\\" + key))
            {
                BitmapImage bmp = new BitmapImage();
                using (IsolatedStorageFileStream fs = new IsolatedStorageFileStream("cache\\" + key, FileMode.Open, _pageIsoStoFile))
                {
                    bmp.SetSource(fs);
                    fs.Flush();
                    fs.Close();
                }
                return bmp;
            }
            else
                return null;
        }
        /// <summary>
        /// 缓存图片.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public bool CacheImage(string key, Stream str)
        {
            try
            {
                IsolatedStorageFileStream fs = new IsolatedStorageFileStream("cache\\" + key, FileMode.OpenOrCreate, _pageIsoStoFile);
                byte[] bytes = new byte[str.Length];
                str.Read(bytes, 0, bytes.Length);
                str.Seek(0, SeekOrigin.Begin);
                fs.Write(bytes, 0, bytes.Length);
                fs.Flush();
                fs.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        #region
        ///// <summary>
        ///// 获取或设置文件缓存
        ///// </summary>
        ///// <param name="key"></param>
        ///// <returns></returns>
        //public BitmapImage this[string key]
        //{
        //    get
        //    {
        //        if (_pageIsoStoFile.FileExists("cache/" + key))
        //        {
        //            BitmapImage bmp = new BitmapImage();
        //            using (IsolatedStorageFileStream fs = new IsolatedStorageFileStream("cache/" + key, FileMode.Open, _pageIsoStoFile))
        //            {
        //                bmp.SetSource(fs);
        //                fs.Flush();
        //                fs.Close();
        //            }
        //            return bmp;
        //        }
        //        else
        //            return null;
        //    }
        //    set
        //    {
        //        try
        //        {
        //            IsolatedStorageFileStream fs = new IsolatedStorageFileStream("cache/" + key, FileMode.OpenOrCreate, _pageIsoStoFile);
        //            //StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
        //            byte[] bytes = new byte[value];
        //            value.Read(bytes, 0, bytes.Length);
        //            foreach (byte b in bytes)
        //            {
        //                fs.WriteByte(b);
        //            }
        //            fs.Flush();
        //            fs.Close();                    
        //        }
        //        catch
        //        {
        //           // MessageBox.Show("")
        //        }
        //    }
        //}
        #endregion
    }
}
